package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BusinessException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageBean;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    /**
     * 菜品分页
     *
     * @param dishPageDTO
     * @return
     */
    @Override
    public PageBean<DishVO> findByPage(DishPageDTO dishPageDTO) {

        //1 开启分页
        PageHelper.startPage(dishPageDTO.getPage(), dishPageDTO.getPageSize());

        //2 执行查询 ( 调用mapper )
        List<DishVO> list = dishMapper.findList(dishPageDTO);
        Page<DishVO> page = (Page<DishVO>) list;
        //3 返回分页
        return new PageBean<>(page.getTotal(), page.getResult());
    }


    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Transactional //开启事务  (涉及到多个表时)
    @Override
    public void save(DishDTO dishDTO) {
        //1 从dto中提取菜品信息,保存到dish表,返回主键
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        dishMapper.save(dish);
        //2-1 从dto中提取菜品口味List,
        List<DishFlavor> flavorsList = dishDTO.getFlavors();

        //2-2 遍历列表得到每个口味
        if (CollectionUtil.isNotEmpty(flavorsList)) {  //等同 if(flavorsList != null && flavorsList.size>0)
            flavorsList.stream().forEach(dishFlavor -> {

                //2-3 为每个口味设置好 dish_id  (dishId)
                dishFlavor.setDishId(dish.getId());

                //2-4 执行保存
                dishFlavorMapper.save(dishFlavor);
            });
        }


    }

    /**
     * 回显菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO findById(Long id) {

        //1 先查菜品和分类名称 (从菜品表和分类表根据id查询) 分类名称可以在dish表中根据category_id找到
        DishVO dishVO = dishMapper.findById(id);

        //2 再差口味 (从口味表中查)
        List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(id);

        //3 组装返回VO
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional //开启事务
    @Override
    public void update(DishDTO dishDTO) {
        //1 从DishDTO中提取菜品信息,保存到dish表中
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        dishMapper.update(dish);

        //2 先删除当前菜品在口味表中的所有记录(删除旧口味)
        dishFlavorMapper.deleteByDishId(dish.getId());

        //3 从dto中提取到新的口味列表,重新保存到口味表(添加新口味)
        List<DishFlavor> flavorList = dishDTO.getFlavors();

        // 遍历列表的到每个口味 (与新增菜品代码一致)
        if (CollectionUtil.isNotEmpty(flavorList)) {////等同 if(flavorsList != null && flavorsList.size>0)
            flavorList.stream().forEach(dishFlavor -> {
                //2-3. 为每个口味设置好dish_id
                dishFlavor.setDishId(dish.getId());
                //2-4 执行保存
                dishFlavorMapper.save(dishFlavor);
            });
        }
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Autowired //注入
    private SetmealDishMapper setmealDishMapper;

    @Transactional //开启事务
    @Override
    public void delete(List<Long> ids) {
        //1 遍历ids 得到id(菜品主键)
        if (CollectionUtil.isNotEmpty(ids)) {
            ids.stream().forEach(id -> {
                //2 根据id查询菜品信息  (调用mapper)
                DishVO dishVO = dishMapper.findById(id);//这里复用之前的回显菜品(根据id查询)
                //3判断菜品状态 如果是起售状态,无法删除  判断状态,如果是起售,抛异常
                if (dishVO.getStatus() == StatusConstant.ENABLE) {
                    throw new BusinessException("菜品处于起售状态,无法删除");
                }
                //4 判断菜品是否与套餐有关 todo (待做)
                // 检查菜品是否与套餐有关
                if (setmealDishMapper.existsByDishId(id)) {
                    throw new BusinessException("菜品与套餐有关联，无法删除");
                }

                //5 删除菜品 (调用mapper)
                dishMapper.delete(id);
                //6 删除口味 (调用mapper)
                dishFlavorMapper.deleteByDishId(id);//复用之前修改菜品
            });
        }

    }

    /**
     * 起售停售菜品
     *
     * @param dish
     */
    @Override
    public void startOrStop(Dish dish) {
        //调用Mapper
        dishMapper.update(dish);//起售停售菜品本身也算是修改(直接复用)

    }

    /**
     * 查询菜品列表
     *
     * @param dishPageDTO
     * @return
     */
    @Override
    public List<DishVO> findList(DishPageDTO dishPageDTO) {
        //1 查询菜品列表 (调用mapper)
        List<DishVO> dishVOList = dishMapper.findList(dishPageDTO);
        //2 遍历list,得到每个菜品,然后再去查询它的口味列表
        if (CollectionUtil.isNotEmpty(dishVOList)) {
            dishVOList.stream().forEach(dishVO -> {
                //2-2 根据菜品id查询口味列表
                List<DishFlavor> dishFlavorList = dishFlavorMapper.findByDishId(dishVO.getId());
                //2-3 将口味列表设置到菜品VO中
                dishVO.setFlavors(dishFlavorList);
            });

        }

        return dishVOList;//复用之前的条件查询(菜品分页)
    }
}
